//
//  DetailViewOwner.m
//  EcmDoctor
//
//  Created by 张进 on 15/6/4.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "DetailView.h"

@implementation DetailView

@synthesize imageView,topView,leftBottomDate,middleDesc,name,sex,age,bqmsStr,rightBottomPicNum,rightBottomVoiceNum,voiceView,picView,picVoiceView,dWidth,dHeight;

//- (void)awakeFromNib
//{
//    [[NSBundle mainBundle] loadNibNamed:@"DetailView" owner:self options:nil];
//    [self addSubview:self.imageView];
//}
//+ (instancetype)viewFromNIB{
//    DetailView *view = [[DetailView alloc] initWithFrame:CGRectMake(0, 0, 200, 360)];
//    [view awakeFromNib];
//    return view;
//}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        //[self.topView setFrame:CGRectMake(0, 0, self.frame.size.width, self.topView.frame.size.height)];
        [self setBackgroundColor:[UIColor colorWithRed:1.0 green:248.0/255 blue:238.0/255 alpha:1.0]];

    }
    return self;
}

+(DetailView *)instanceDetailView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"DetailView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

//解决 transform 引起的 Constraint 冲突
- (void)layoutSubviews;{
    [super layoutSubviews];
//    [self.topView setFrame:CGRectMake(0, 0, self.frame.size.width, self.topView.frame.size.height)];
    [self.topView setFrame:CGRectMake(0, 0, dWidth, self.topView.frame.size.height)];
    NSLog(@"height:%f,width:%f",self.frame.size.height,self.frame.size.width);
    
    self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2;
    self.imageView.clipsToBounds = YES;
    self.imageView.layer.borderWidth = 3.0f;
    self.imageView.layer.borderColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0
                                        ].CGColor;
    
    //底部日期重新布局
    [self.leftBottomDate setFrame:CGRectMake(15, dHeight-self.leftBottomDate.frame.size.height-15, self.leftBottomDate.frame.size.width, self.leftBottomDate.frame.size.height)];
    
    [self.bzView setFrame:CGRectMake(dWidth-self.bzView.frame.size.width-10,15,  self.bzView.frame.size.width, self.bzView.frame.size.height)];
    [self bringSubviewToFront:self.bzView];
    //右下角图片和声音数量
    CGRect rect =self.picVoiceView.frame;
    rect.origin.x = dWidth-rect.size.width;
    rect.origin.y = dHeight-rect.size.height-15;
    self.picVoiceView.frame = rect;
    
       //垂直居上--病情描述
    [self.middleDesc setFrame:CGRectMake(15, self.bqmsStr.frame.origin.y+self.bqmsStr.frame.size.height+8, dWidth-30, self.leftBottomDate.frame.origin.y-self.bqmsStr.frame.origin.y-self.bqmsStr.frame.size.height-8)];
    
    [self.middleDesc sizeToFit];
    
    
//    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]];
    
}

-(void)labelTap{
    UIViewController *controller = [self findViewController:self];
    if (controller) {
        
        [controller performSegueWithIdentifier:@"toDetail" sender:controller];
    }
}

- (UIViewController *)findViewController:(UIView *)sourceView
{
    id target=sourceView;
    while (target) {
        target = ((UIResponder *)target).nextResponder;
        if ([target isKindOfClass:[UIViewController class]]) {
            break;
        }
    }
    return target;
}

@end
